package com.ruoyi.restaurant.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.restaurant.domain.ResShop;
import com.ruoyi.restaurant.service.IResShopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.restaurant.domain.ResOrderList;
import com.ruoyi.restaurant.service.IResOrderListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author adnaan
 * @date 2023-12-11
 */
@RestController
@RequestMapping("/restaurant/list")
public class ResOrderListController extends BaseController
{
    @Autowired
    private IResOrderListService resOrderListService;


    @Autowired
    private IResShopService resShopService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('restaurant:list:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResOrderList resOrderList)
    {
        Long userId = SecurityUtils.getUserId();
        if(!SecurityUtils.isAdmin(userId)){
            ResShop resShop = new ResShop();
            resShop.setOwnerId(userId);
            List<ResShop> list = resShopService.selectResShopList(resShop);
            if(list.isEmpty()){
                System.out.println("该用户没有店铺");
            }else{
                ResShop shop = list.get(0);
                resOrderList.setShopId(shop.getId());
            }
        }
        startPage();
        List<ResOrderList> list = resOrderListService.selectResOrderListList(resOrderList);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('restaurant:list:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResOrderList resOrderList)
    {
        List<ResOrderList> list = resOrderListService.selectResOrderListList(resOrderList);
        ExcelUtil<ResOrderList> util = new ExcelUtil<ResOrderList>(ResOrderList.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('restaurant:list:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(resOrderListService.selectResOrderListById(id));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('restaurant:list:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResOrderList resOrderList)
    {
        return toAjax(resOrderListService.insertResOrderList(resOrderList));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('restaurant:list:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResOrderList resOrderList)
    {
        return toAjax(resOrderListService.updateResOrderList(resOrderList));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('restaurant:list:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(resOrderListService.deleteResOrderListByIds(ids));
    }
}
